package chenji.moreitemfunapi.textures;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import org.bukkit.entity.Player;

import com.google.common.collect.Maps;
import chenji.moreitemfunapi.MifAPI;
import chenji.moreitemfunapi.textures.info.TextureInfo;
import io.netty.buffer.ByteBuf;

public class UpdateManager {
	private final HashMap<Player, TextureUpdateInfo> record = Maps.newHashMap();
	private final File root = new File(MifAPI.getMDataFolder() + "/textures");
	private static File packFile = new File(MifAPI.getMDataFolder() + "/pack.zip");

	public UpdateManager() {
		if (!root.exists()) {
			root.mkdirs();
		}
	}

	private void sendRequest(Player p, TextureUpdateInfo info) {
		p.sendMessage("§a[MIFAPI]§c已经向您发送更改/设定物品 §f" + info.info.getItemName() + " §c材质的请求。\n§c请在客户端Mod弹出的窗口内选择文件。");
		this.record.put(p, info);
		MifAPI.getPluginMessageManager().setGetTexturePackage(info.name, p);
	}

	public void sendRequest(Player p, TextureInfo info, String name) {
		TextureUpdateInfo infoa = TextureUpdateInfo.getInfo(info, name);
		if (infoa == null) {
			p.sendMessage("你所指定的名称不包含");
			return;
		}
		this.sendRequest(p, infoa);
	}

	public void sendRequest(Player p, int UID, String name) {
		if (!MifAPI.getTexturesManager().contain(UID)) {
			p.sendMessage("你所指定的UID不存在");
			return;
		}
		this.sendRequest(p, MifAPI.getTexturesManager().getTextureInfo(UID), name);
	}

	public void sendPack(Player p) {
		p.sendMessage("§a[MIFAPI]§c正在发送材质包");
		if (!packFile.exists()) {
			p.sendMessage("未生成材质包，请先生成后再下载。");
			return;
		}
		try (FileInputStream stream = new FileInputStream(packFile);) {
			int size = stream.available();
			int packageamount = size / 30000;
			int lastPacksize = size - 30000 * packageamount;
			int i = 0;
			MifAPI.getPluginMessageManager().sendHeadPack(p, packageamount + 1);
			while (i < packageamount) {
				i++;
				byte[] data = new byte[30000];
				stream.read(data);
				MifAPI.getPluginMessageManager().sendPack(p, i, data);
			}

			byte[] data = new byte[lastPacksize];
			stream.read(data);
			MifAPI.getPluginMessageManager().sendPack(p, packageamount + 1, data);
		} catch (IOException e) {
			p.sendMessage("出现异常，请参见后台");
			e.printStackTrace();
			return;
		}

	}

	public boolean contain(Player arg1) {
		return this.record.containsKey(arg1);
	}

	private void dealFile(File f) throws IOException {
		if (f.exists()) {
			f.delete();
		}
		f.createNewFile();
	}

	public void dealStateThree(Player p, ByteBuf buf) {
		String name = this.record.get(p).name;
		TextureInfo info = this.record.get(p).info;
		File f = new File(this.root + "/" + info.getImageName(name) + ".png");
		try {
			dealFile(f);
		} catch (IOException e) {
			p.sendMessage("§a[MIFAPI]§c创建文件时出现异常，请检查服务器对应目录 " + f + " 是否被占用。");
			e.printStackTrace();
			return;
		}

		int length = buf.readInt();
		byte[] data = new byte[length];
		buf.readBytes(data);
		FileOutputStream stream = null;
		try {
			stream = new FileOutputStream(f);
			stream.write(data);
			stream.flush();
			p.sendMessage("§a[MIFAPI]§c成功接收物品 §f" + name + " §c的材质。");
		} catch (IOException e) {
			p.sendMessage("§a[MIFAPI]§c创建文件时出现异常，请检查服务器对应目录 " + f + " 是否被占用。");
			e.printStackTrace();
			return;
		} finally {
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void dealOtherPackage(int state, Player p) {
		switch (state) {
		case 2:
			p.sendMessage("§a[MIFAPI]§c材质文件仅限PNG类型的图片。");
			return;
		case 4:
			p.sendMessage("§a[MIFAPI]§c您取消了选择材质");
			return;
		case 5:
			p.sendMessage("§a[MIFAPI]§c访问被拒绝，无法读取您选定的材质。");
			return;
		default:
			p.sendMessage("§a[MIFAPI]§c无效的数据包，无法读取");
			return;
		}
	}
}
